/*#include <REGX52.H>
#include<intrins.h>

void time(char a)
{
	while(a--)
	{
		unsigned char i;

	_nop_();
	i = 25;
	while (--i);
	}	
}

int main()
{
	int i = 0;
	P2 = 0x0;
	
	 while(1)
	 {
	     for(i=10;i<=1000;++i)
		   {
		       P2=0;		     
		       time(i); 
		       P2 =0xff;         
		       time(2000-i);
		   }			           
		   for(i=1000;i>=10;--i)
		   {
		       P2 = 0;		    
		       time(i); 
		       P2 = 0xff;       
		       time(2000-i);
		   }	              
		 }
	return 0;
}*/
#include<stdlib.h>
#include <REGX52.H>
#include"lcd1602.h"
#include<intrins.h>
#include"time.h"

void time(char a)
{
	while(a--)
	{
		_nop_();
		_nop_();
		_nop_();
	}
}


char key()
{
	char b = 0;
	char a = 0;
	P1 = 0x0f;
	if(P1_0 == 0)
		a = 4;
	else if(P1_1 == 0)
		a = 3;
	else if(P1_2 == 0)
		a = 2;
	else if(P1_3 == 0)
		a = 1;
	else 
		return 0;
	time(1);
	P1 = 0xf0;
	if(P1_4 == 0)
		a += 12;
	else if(P1_5 == 0)
		a += 8;
	else if(P1_6 == 0)
		a += 4;
	
	while(b < 50 && P1 != 0xf0)
	{
		time(100000);
		b++;
	}
	
	if(b >= 50)
		return 0;
	return a;
}    

int main()
{
	char x;
	char y;
	int z = 0;
	char k = 0;
	int t;
	LCD_Init();
	srand((unsigned int)time(0));
	//srand((unsigned int)get_timer_value());
	srand(1); 
	while(1)
	{
			while(1)
			{
			k = 0;
			x = rand()%4 + 1;
			y = rand()%4 + 1;
			LCD_ShowNum(2,1,x,2);
			LCD_ShowNum(2,4,y,2);
			
			while(!k)
			{
				k = key();
			}
			
			z = (y - 1)*4 + x;
			
			if(k == z)
			{
				LCD_Init();
				continue;
			}
			else 
			{
				LCD_Init();
				LCD_ShowString(1,1,"game_over");
				k = 0;
				t = 1000;
				while(t--)
					time(1000);
				break;
			}
		}
		LCD_Init();
		LCD_ShowChar(1,1,"continue?");
		LCD_ShowChar(2,1,"Y or N(1/2)");
		k = key();
		if(k == 1)
			continue;
		else 
			break;
	}
	return 0;
}
/*
int main()
{
	char x;
	char y;
	LCD_Init();
	srand((unsigned int)get_timer_value());
	x = rand()%4 + 1;
	y = rand()%4 + 1;
	LCD_ShowNum(2,1,1,2);
	LCD_ShowNum(2,4,y,2);
	while(1);
	return 0;
}
*/
/*
int main()
{
	char x = 1;
	LCD_Init();
	srand((unsigned int)get_timer_value());
	LCD_ShowNum(1,1,1,1);
	while(1)
	{;}
}
*/
